//
//  IndexController.m
//  linlee-iotios
//
//  Created by mengze on 2025/5/25.
//

#import <Foundation/Foundation.h>
#import "LoginController.h"
#import <Masonry/Masonry.h>
#import "UIColor+Hex.h"
#import "IndexController.h"

@interface LoginController()

@property (nonatomic, strong) UIImageView * logoImageView;
@property (nonatomic, strong) UITextField *usernameField;
@property (nonatomic, strong) UITextField *passwordField;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIImageView *logoImgView;


@end


@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    
    [self initView];
    [self initViewData];
}



- ( void ) initView {
    
    // 创建UIImageView
    self.logoImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
    self.logoImgView.backgroundColor = [UIColor lightGrayColor];
    self.logoImgView.layer.cornerRadius = 6;
    self.logoImgView.layer.masksToBounds = YES;
    self.logoImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:self.logoImgView];
    
    // 用户名输入框
    self.usernameField = [[UITextField alloc] init];
    self.usernameField.placeholder = @"请输入用户名";
    self.usernameField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.usernameField];
    
    // 密码输入框
    self.passwordField = [[UITextField alloc] init];
    self.passwordField.placeholder = @"请输入密码";
    self.passwordField.secureTextEntry = YES;
    self.passwordField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.passwordField];
    
    // 登录按钮
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginButton];
    
    
    self.loginButton.backgroundColor = [UIColor greenColor];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.loginButton.font = [UIFont systemFontOfSize:18];
    self.loginButton.layer.cornerRadius = 6;
    self.loginButton.layer.masksToBounds = YES;
    
    [self.loginButton addTarget:self action:@selector(toLogin:) forControlEvents:UIControlEventTouchUpInside];
}


- ( void ) toLogin: (id) sender {
    IndexController * vc = [[IndexController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}



- ( void ) initViewData {
    
    
    // Masonry布局
    [self.logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.top.equalTo(self.view.mas_top).offset(120);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
    
    // 使用Masonry布局
    [self.usernameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImgView.mas_bottom).offset(60);
        make.left.equalTo( self.view ).offset(20);
        make.right.equalTo( self.view ).offset(-20);
        make.height.equalTo( @52 );
//        make.width.equalTo(@200);
//        make.height.equalTo(@40);
    }];
    
    UIView * usernameLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];
    self.usernameField.leftView= usernameLeftView;
    self.usernameField.leftViewMode = UITextFieldViewModeAlways;
    
    
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.usernameField.mas_bottom).offset(20);
        make.width.equalTo(self.usernameField);
        make.height.equalTo(self.usernameField);
    }];
    UIView * passwordLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];
    self.passwordField.leftView= passwordLeftView;
    self.passwordField.leftViewMode = UITextFieldViewModeAlways;
    
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordField.mas_bottom).offset(30);
        make.width.equalTo(self.usernameField);
        make.height.equalTo(self.usernameField);
        make.centerX.equalTo(self.usernameField );
        make.height.equalTo(@52);
    }];
    
 
}

- (void)loginAction {
    NSLog(@"点击了登录按钮");
    // 这里添加登录逻辑
}

@end
